import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

import java.util.ArrayList;

public class CreateTable {
    public static void main(String[] args) {

        // 指定表连接到的master节点主机名
        String masterAdd = "server2";
        KuduClient.KuduClientBuilder kcb = new KuduClient.KuduClientBuilder(masterAdd);
        KuduClient client = kcb.build();

        // 定义schema
        // 指定表名
        String tableName = "student";
        // 指定列的信息
        //id name
        ArrayList<ColumnSchema> columnSchemas = new ArrayList<ColumnSchema>();
        ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build();
        ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).build();
        columnSchemas.add(id);
        columnSchemas.add(name);
        Schema schema = new Schema(columnSchemas);

        // 指定副本数量、分区策略和数量
        CreateTableOptions options = new CreateTableOptions();
        options.setNumReplicas(1);
        ArrayList<String> colRule = new ArrayList<String>();
        colRule.add("id");
        options.addHashPartitions(colRule,3);

        //建表
        try {
            client.createTable(tableName, schema, options);
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
}


